package table

import (
	"errors"
	"fmt"
	"yunj/pkg/yunj/app/enum/stateenum"
	"yunj/pkg/yunj/core/builder"
	"yunj/pkg/yunj/core/builder/builderdto"
	"yunj/pkg/yunj/core/builder/form/field"
	"yunj/pkg/yunj/core/builder/table"
	"yunj/pkg/yunj/core/builder/table/col"
	"yunj/pkg/yunj/core/builder/table/col/action"
	"yunj/pkg/yunj/core/builder/table/deftoolbar"
	"yunj/pkg/yunj/core/builder/table/toolbar"

	"github.com/gin-gonic/gin"
)

// 示例
func CommonTableBuilder(ctx *gin.Context, id string) *builder.Table {
	return builder.NewTable(ctx, id).
		State([]builderdto.TableState{{Code: stateenum.NORMAL, Title: "正常"}, {Code: stateenum.RECYLE_BIN, Title: "回收站"}}).
		TitleCol("name").
		Page(false).
		Toolbar(func(t *builder.Table, state *builderdto.TableState) (actions []toolbar.ActionInterface, err error) {
			actions = []toolbar.ActionInterface{
				toolbar.NewOpenPopup("add", "添加").SetClass("layui-icon-add-circle").SetUrl("/admin/demo/table/add"),
			}
			switch state.Code {
			case stateenum.NORMAL:
				actions = append(actions, toolbar.NewAsyncEvent(stateenum.RECYLE_BIN, "移入回收站").SetDropdown(true))
			case stateenum.RECYLE_BIN:
				actions = append(
					actions,
					toolbar.NewAsyncEvent(stateenum.NORMAL, "恢复正常").SetDropdown(true),
					toolbar.NewAsyncEvent(stateenum.DELETED, "永久删除").SetDropdown(true).SetConfirmFormFields(field.NewTxt("tips", "确认删除？")),
				)
			}
			return
		}).
		DefaultToolbar([]deftoolbar.ActionInterface{
			deftoolbar.NewFilter(),
		}).
		Event(func(t *builder.Table, requestParam *builderdto.TableRequestParam) (res builderdto.TableEventResult, err error) {
			fmt.Printf("event: %v,%T\r\n", requestParam.EventCode, requestParam.EventCode)
			err = errors.New("演示数据不支持该操作")
			return
		})
}

func CommonCols(ctx *gin.Context, state *builderdto.TableState, isTree bool) (cols []table.ColInterface, err error) {
	cols = []table.ColInterface{
		col.NewCheckbox("id", "ID"),
	}
	if !isTree {
		cols = append(cols, col.NewDragSort("name", "姓名"))
	} else {
		cols = append(cols, col.NewText("name", "姓名"))
	}
	cols = append(cols, []table.ColInterface{
		col.NewText("age", "年龄"),
		col.NewEnum("grade", "年级").SetOptions([]builderdto.TableColOption{
			{Value: 1, Label: "一年级", BgColor: "#FFB6C1"},
			{Value: 2, Label: "二年级", BgColor: "#FFA500"},
			{Value: 3, Label: "三年级", BgColor: "#FF0000"},
			{Value: 4, Label: "四年级", BgColor: "#A52A2A"},
		}),
		col.NewDatetime("created_at", "入学时间"),
		col.NewImage("avatar", "头像"),
		col.NewImage("album", "相册"),
		col.NewFile("personal_profile", "个人档案"),
		col.NewFile("other_profile", "其他资料"),
		col.NewColor("color", "喜欢的颜色"),
		col.NewIcon("icon", "喜欢的图标"),
	}...)
	actionCol := col.NewAction("操作").SetOptions([]action.ActionInterface{
		action.NewOpenPopup("edit", "详情").SetClass("layui-icon-survey").SetUrl("..."),
	})
	switch state.Code {
	case stateenum.NORMAL:
		actionCol.Options = append(actionCol.Options,
			action.NewAsyncEvent(stateenum.RECYLE_BIN, "移入回收站").SetDropdown(true))
	case stateenum.RECYLE_BIN:
		actionCol.Options = append(actionCol.Options,
			action.NewAsyncEvent(stateenum.NORMAL, "还原").SetDropdown(true),
			action.NewAsyncEvent(stateenum.DELETED, "永久删除").SetDropdown(true).SetConfirmFormFields(field.NewTxt("tips", "确认删除？")),
		)
	}
	cols = append(cols, actionCol)
	return
}
